/*
 * BrowsingHistoryFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.browsing;

import android.support.design.widget.TabLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.ui.adapter.BrowsingHistoryAdapter;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.utils.RealmUtils;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import io.realm.Realm;

/** 我的 浏览历史 */
public class BrowsingHistoryFragment extends SuperBaseFragment
        implements BrowsingHistoryContract.View {
    private static final String Tag = BrowsingHistoryFragment.class.getSimpleName();

    private BrowsingHistoryContract.Presenter mPresenter;

    private ListView mListView;

    private PtrClassicFrameLayout mPtrFrame;

    private BrowsingHistoryAdapter mBrowsingHistoryAdapter;

    private Toolbar mToolbar;

    private TextView mTvClear;

    private Realm mHistoryRealm;

    private LoadingDialog mLoadingDialog;

    private LoadMoreListViewContainer mLoadMoreContainer;

    private TabLayout mTabLayout;

    private TabLayout.Tab mTab;

    public static BrowsingHistoryFragment getInstance() {
        return new BrowsingHistoryFragment();
    }

    @Override
    protected void initViews() {

        mTabLayout = (TabLayout) mRootView.findViewById(R.id.tab_layout);

        mLoadMoreContainer =
                (LoadMoreListViewContainer) mRootView.findViewById(R.id.load_more_container);

        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);

        mBrowsingHistoryAdapter = new BrowsingHistoryAdapter(mActivity);
        mListView.setAdapter(mBrowsingHistoryAdapter);

        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTvClear = (TextView) mRootView.findViewById(R.id.tv_browsing_delete);

        initEvent();
        mPtrFrame.setLastUpdateTimeRelateObject(this);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initEvent() {
        mTabLayout.addOnTabSelectedListener(
                new TabLayout.OnTabSelectedListener() {

                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        mTab = tab;

                        mPresenter.changeData(tab.getText() + "");
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                    }
                });

        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });
        mTvClear.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UmengUtils.onEvent(mActivity, R.string.umeng_browsing_clear);
                        mPresenter.deleteAll();
                    }
                });

        // 下拉刷新
        mPtrFrame.setPtrHandler(
                new PtrDefaultHandler() {
                    @Override
                    public boolean checkCanDoRefresh(
                            PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(
                                frame, mListView, header);
                    }

                    @Override
                    public void onRefreshBegin(PtrFrameLayout frame) {
                        mPresenter.changeData(mTab.getText() + "");
                    }
                });
        // 上拉加载更多
        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        mPresenter.getHistoryData(false);
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_browing_history;
    }

    @Override
    public void setPresenter(BrowsingHistoryContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return Tag;
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    public void refreshUI() {
        mTvClear.setVisibility(View.GONE);
        mBrowsingHistoryAdapter.notifyDataSetChanged();
    }

    @Override
    public TabLayout getTabLayout() {
        return mTabLayout;
    }

    @Override
    public void setDisPlayItems(List<DisplayListItem> disPlayItems) {

        mBrowsingHistoryAdapter.setData(disPlayItems);
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public Realm getRealm() {
        mHistoryRealm = RealmUtils.getHistoryRealm(mActivity);
        return mHistoryRealm;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHistoryRealm.close(); //关闭数据库
        mPresenter.destroy(); //移除回调
    }

    @Override
    public void showDialog() {
        mLoadingDialog = new LoadingDialog(mActivity);
        mLoadingDialog.show();
    }

    @Override
    public void dismissDialog() {
        mLoadingDialog.dismiss();
    }

    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public void setTvClearVisible(boolean visible) {
        mTvClear.setVisibility(visible ? View.VISIBLE : View.GONE);
    }
}
